// Windows/FileDir.h

#ifndef __WINDOWS_FILEDIR_H
#define __WINDOWS_FILEDIR_H


#include "Common/System/Defs.h"

namespace Common {
namespace FileSystem {

#ifdef WIN_LONG_PATH
bool GetLongPaths(LPCTSTR s1, LPCTSTR s2, CString &d1, CString &d2);
#endif

bool GetWindowsDirectory(CString &path);
bool GetSystemDirectory(CString &path);

bool SetDirTime(LPCWSTR fileName, const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime);

bool SetFileAttributes(LPCTSTR fileName, DWORD fileAttributes);
bool MoveFile(LPCTSTR existFileName, LPCTSTR newFileName);
bool RemoveDirectory(LPCTSTR pathName);
bool CreateDirectory(LPCTSTR pathName);
bool CreateComplexDirectory(LPCTSTR pathName);
bool DeleteFileAlways(LPCTSTR name);
bool RemoveDirectoryWithSubItems(const CString &path);

bool GetOnlyDirPrefix(LPCTSTR fileName, CString &resultName);
bool GetOnlyName(LPCTSTR fileName, CString &resultName);
bool GetShortPathName(LPCTSTR longPath, CString &shortPath);

bool GetFullPathName(LPCTSTR fileName, CString &resultPath, int &fileNamePartStartIndex);
bool GetFullPathName(LPCTSTR fileName, CString &resultPath);

inline bool SetCurrentDirectory(LPCTSTR path)
  { return BOOLToBool(::SetCurrentDirectory(path)); }
bool GetCurrentDirectory(CString &resultPath);

bool SearchPath(LPCTSTR path, LPCTSTR fileName, LPCTSTR extension, CString &resultPath, UINT32 &filePart);

inline bool SearchPath(LPCTSTR path, LPCTSTR fileName, LPCTSTR extension, CString &resultPath)
{
  UINT32 value;
  return SearchPath(path, fileName, extension, resultPath, value);
}

bool GetTempPath(CString &resultPath);

UINT GetTempFileName(LPCTSTR dirPath, LPCTSTR prefix, CString &resultPath);

class CTempFile
{
  bool _mustBeDeleted;
  CString _fileName;
public:
  CTempFile(): _mustBeDeleted(false) {}
  ~CTempFile() { Remove(); }
  void DisableDeleting() { _mustBeDeleted = false; }
  UINT Create(LPCTSTR dirPath, LPCTSTR prefix, CString &resultPath);
  bool Create(LPCTSTR prefix, CString &resultPath);
  bool Remove();
};

bool CreateTempDirectory(LPCTSTR prefixChars, CString &dirName);

class CTempDirectory
{
  bool _mustBeDeleted;
  CString _tempDir;
public:
  const CString &GetPath() const { return _tempDir; }
  CTempDirectory(): _mustBeDeleted(false) {}
  ~CTempDirectory() { Remove();  }
  bool Create(LPCTSTR prefix) ;
  bool Remove()
  {
    if (!_mustBeDeleted)
      return true;
    _mustBeDeleted = !RemoveDirectoryWithSubItems(_tempDir);
    return (!_mustBeDeleted);
  }
  void DisableDeleting() { _mustBeDeleted = false; }
};

}}

#endif
